package com.zp.drug.controller;

import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.drug.entity.bo.DrugInventoryCheckBo;
import com.zp.drug.entity.vo.DrugInventoryCheckVo;
import com.zp.drug.service.IDrugInventoryCheckService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 药品盘点表控制器
 * 前端访问路由地址为:/drug/drugInventoryCheck
 *
 * @author zhang peng
 * @ date 2024-06-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/drugInventoryCheck")
public class DrugInventoryCheckController extends BaseController {

    private final IDrugInventoryCheckService iDrugInventoryCheckService;

    /**
     * 查询药品盘点表列表 (分页)
     */
    @GetMapping("/selectPage")
    public TableDataInfo<DrugInventoryCheckVo> selectPage(DrugInventoryCheckBo bo, PageQuery pageQuery) {
        return iDrugInventoryCheckService.queryPageList(bo, pageQuery);
    }

    /**
     * 按申请单分组查询盘点记录
     */
    @GetMapping("/selectGroupInventoryNo")
    public TableDataInfo<DrugInventoryCheckVo> selectGroupInventoryNo(DrugInventoryCheckBo bo, PageQuery pageQuery) {
        return iDrugInventoryCheckService.selectGroupInventoryNo(bo, pageQuery);
    }

    /**
     * 查询药品盘点表列表 (不分页)
     */
    @GetMapping("/list")
    public R<List<DrugInventoryCheckVo>> list(DrugInventoryCheckBo bo) {
        List<DrugInventoryCheckVo> list = iDrugInventoryCheckService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出药品盘点表列表
     */
    @Log(title = "药品盘点表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DrugInventoryCheckBo bo, HttpServletResponse response) {
        List<DrugInventoryCheckVo> list = iDrugInventoryCheckService.queryList(bo);
        ExcelUtil.exportExcel(list, "药品盘点表", DrugInventoryCheckVo.class, response);
    }

    /**
     * 获取药品盘点表详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<DrugInventoryCheckVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iDrugInventoryCheckService.queryById(id));
    }

    /**
     * 新增药品盘点表
     */
    @Log(title = "药品盘点表", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DrugInventoryCheckBo bo) {
        return toAjax(iDrugInventoryCheckService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改药品盘点表
     */
    @Log(title = "药品盘点表", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DrugInventoryCheckBo bo) {
        return toAjax(iDrugInventoryCheckService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除药品盘点表
     *
     * @param ids 主键串
     */
    @Log(title = "药品盘点表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{inventoryNo}/{orgId}")
    public R<Void> remove(@PathVariable String inventoryNo, @PathVariable String orgId) {
        return toAjax(iDrugInventoryCheckService.deleteWithValidByInventoryNo(inventoryNo, orgId) ? 1 : 0);
    }


    /**
     * 库存调整
     *
     * @param inventoryNo
     * @param orgId
     * @return
     */
    @GetMapping("/stockAdjust/{inventoryNo}/{orgId}")
    public R<Void> stockAdjust(@NotNull(message = "盘点单号不能为空") @PathVariable String inventoryNo,
                               @NotNull(message = "机构id不能为空") @PathVariable String orgId) {
        return toAjax(iDrugInventoryCheckService.stockAdjust(inventoryNo, orgId));
    }

}
